/*cI4u54VYZVPxnvGrX5EL6HXX6+tBhMNbry4zRW5Uf2RuXYfnoeOdBS4xA3fBBRYR*/
/**
 * 固定资产卡片 列表态
 * @author zhangypm
 * @since ncc1.0 2018年5月11日
 */
import React, {Component} from 'react';
import {createPage, ajax, base, high, cardCache, cacheTools, createPageIcon } from 'nc-lightapp-front';
import constants from '../constants';
import ampub from 'ampub';
const {components, utils} = ampub;
const {showMessage} = utils.msgUtils;
const {createListTitleArea, creatListSocket} = utils.listUtils;
const {queryVoucherSrc} = components.queryVocherSrcUtils;
const {getMultiLangByID} = utils.multiLangUtils;

import {
    searchBtnClick,
    clickAdvBtnEve,
    pageInfoClick,
    initTemplate,
    buttonClick,
    doubleClick,
    afterEvent,
    rowSelected,
    linkedQueryUtil,
} from './events';

import FaCardCopy from '../components/CardCopy';
import '../components/CardCopy.less';

const searchId = constants.AREA.LIST.SEARCH_AREA; //查询区
const tableId = constants.AREA.LIST.GRID_AREA; //表格区
const assetDataSource = constants.assetDataSource;
const {NCDiv} = base;

class List extends Component {
    constructor(props) {
        super(props);

        this.facardCopy = new FaCardCopy('list');

        this.state = {};

        this.isFirstLoad = true;//高级按钮第一次点击

        this.templateid = "";//模板主键

        this.doInit.bind(this);

        this.doInit().then(() => {
            initTemplate.call(this, this.props);
        });
    }

    componentDidMount() {
        console.log('---------------------  componentDidMount linkedQueryUtil=', linkedQueryUtil);
        console.log('---------------------  componentDidMount linkedQueryByVoucher=', linkedQueryUtil.linkedQueryByVoucher);

        //凭证联查来源单据
        queryVoucherSrc(this.props, tableId, "pk_card", constants.PAGE_CODE.LIST, linkedQueryUtil.linkedQueryByVoucher.bind(this));

        //其他单据联查卡片到列表态（如：工程转固单按数量拆分建卡生成多张卡片时，联查时需要到列表态）
        linkedQueryUtil.linkedQueryByBill.call(this, this.props);
    }

    //初始化
    doInit() {
        const that = this;
        const promise = new Promise(function (resolve, reject) {
            ajax({
                url: constants.URL.INIT,
                data: {},
                success: (res) => {
                    let {success, data} = res;
                    if (success) {
                        that.state.transiTypeAry = data.billTypeAry;

                        if (data.msgs && data.msgs.length) {
                            showMessage(null, {content: data.msgs.join(','), color: 'warning'});
                        }

                        resolve(data);

                    } else {
                        reject(data);
                    }
                },
                error: (err) => {
                    showMessage(null, {content: err.message, color: 'danger'});
                    reject(err);
                }
            });
        });
        return promise;
    }

    //删除对话框 确认按钮
    delConfirm = () => {
        buttonClick.call(this, this.props, "deleteConfirm");
    };

    render() {
        let {table, search, button, modal, ncmodal, pageConfig, ncUploader} = this.props;
        let {createSimpleTable} = table;
        let {NCCreateSearch} = search;
        let {createNCUploader} = ncUploader;
        let {createModal} = ncmodal;
        let {createButtonApp} = button;

        return (
            <div className="nc-bill-list">
                <NCDiv areaCode={NCDiv.config.HEADER} className='nc-bill-header-area'>
                    {/* 标题区 */}
                    <div className="header-title-search-area">
                        {createListTitleArea.call(this, this.props, {title: pageConfig.title})}
                    </div>

                    <div className="header-button-area">
                        {createButtonApp({
                            area: 'list_head',
                            buttonLimit: 10,
                            onButtonClick: buttonClick.bind(this),
                            popContainer: document.querySelector('.btn-group')
                        })}
                    </div>
                </NCDiv>
                <div className="nc-bill-search-area">
                    {NCCreateSearch(searchId, {
                        clickAdvBtnEve: clickAdvBtnEve.bind(this), //高级按钮点击前事件
                        clickSearchBtn: searchBtnClick.bind(this),
                        onAfterEvent: afterEvent.bind(this),
                    })}
                </div>
                <div className="nc-bill-table-area">
                    {createSimpleTable(tableId, {
                        dataSource: assetDataSource,
                        pkname: 'pk_cardhistory',
                        handlePageInfoChange: pageInfoClick,
                        onSelected: rowSelected.bind(this),
                        onSelectedAll: rowSelected.bind(this),
                        showIndex: true,
                        showCheck: true,
                        onRowDoubleClick: doubleClick.bind(this),
                        componentInitFinished: () => {
                            //缓存数据赋值成功的钩子函数
                            //若初始化数据后需要对数据做修改，可以在这里处理
                            rowSelected.call(this, this.props, tableId);
                        }
                    })}
                </div>

                {modal.createModal(constants.MODAL_ID.CopyModal, {
                    title: getMultiLangByID('201201504A-000026')/* 国际化处理： 卡片复制 */,
                    content: this.facardCopy.copyCardDialog(this),
                    userControl: true,
                    className:'combine',
                })}
                {/* 附件*/}
                {createNCUploader('uploader', {})}
                {/* 创建 socket 链接 */}
                {creatListSocket(this.props, {
                        tableAreaCode: tableId,//表格区域编码
                        billpkname: constants.FIELDS.HEAD_PK,//表格主键
                        //billtype: pageConfig.billType,//单据类型,用于查询追溯
                    }
                )}
            </div>
        );
    }
}

export default List;



/*cI4u54VYZVPxnvGrX5EL6HXX6+tBhMNbry4zRW5Uf2RuXYfnoeOdBS4xA3fBBRYR*/